点击上方 "程序员小乐"关注, 星标或置顶一起成长
每天凌晨00点00分, 第一时间与你相约
每日英文
Never give up on something you really want. It’s difficult to wait, but worse to regret.
永远不要放弃你真正想要的东西。等待虽难,但后悔更甚。
每日掏心话
人生最可悲的是:生活把我们变成了当初最讨厌的那种人,可我们终究还是过不上自己想要的生活。
来自:公众号 读芯术 | 责编:乐乐
链接:dzone.com/articles/14-tips-for-writing-spring-mvc-controller
程序员小乐(ID:study_tech)第 698 次推文 图片来自网络
往日回顾:感受Lambda之美,建议收藏,需要时查阅
正文
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
publicclassHomeController {
@RequestMapping("/")
publicString visitHome() {
// do something before returning view name
return"home";
}
<annotation-driven/>
<context:component-scanbase-package="net.codejava.spring"/>
publicclassMultiActionController {
@RequestMapping("/listUsers")
public ModelAndView listUsers() {
@RequestMapping("/saveUser")
public ModelAndView saveUser(User user) {
@RequestMapping("/deleteUser")
public ModelAndView deleteUser(User user) {
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
publicclassMainControllerimplements Controller {
@Override
public ModelAndView handleRequest(HttpServletRequest request,
HttpServletResponse response) throws Exception {
System.out.println("Welcome main");
returnnew ModelAndView("main");
<beanname="/main"class="net.codejava.spring.MainController"/>
import org.springframework.web.servlet.mvc.AbstractController;
publicclassBigControllerextends AbstractController {
protected ModelAndView handleRequestInternal(HttpServletRequest request,
System.out.println("You're big!");
returnnew ModelAndView("big");
<beanname="/big"class="net.codejava.spring.BigController">
<propertyname="supportedMethods"value="POST"/>
</bean>
@RequestMapping("/login")
import org.springframework.web.bind.annotation.RequestMethod;
@RequestMapping("/hello")
publicclassSingleActionController {
@RequestMapping(method = RequestMethod.GET)
publicString sayHello() {
return"hello";
publicclassUserController {
publicString listUsers() {
return"ListUsers";
publicString saveUser() {
return"EditUser";
publicString deleteUser() {
return"DeleteUser";
@RequestMapping({"/hello", "/hi", "/greetings"})
import org.springframework.web.bind.annotation.RequestMapping
publicclassLoginController {
@RequestMapping(value = "/login", method = RequestMethod.GET)
publicString viewLogin() {
return"LoginForm";
@RequestMapping(value = "/login", method = RequestMethod.POST)
publicString doLogin() {
return"Home";
@RequestMapping(value = "/login", method = RequestMethod.POSTpublic String doLogin(@RequestParamString username @RequestParamString password) {}
@RequestParamint securityNumber
@RequestParam("SSN") int securityNumber
@RequestParam(required = false) String country
@RequestParam(defaultValue = "18") int age
doLogin(@RequestParam Map<String, String> params)
public String viewLogin() {
List<User> listUser = new ArrayList<>();
// get user list from DAO...
ModelAndView modelView = new ModelAndView("UserList");
modelView.addObject("listUser", listUser);
return modelView;
public ModelAndView listUsers(ModelAndView modelView) {
modelView.setViewName("UserList");
modelView.addObject("siteName", newString("CodeJava.net"));
modelView.addObject("users", 1200000);
publicStringviewStats(Map<String, Object> model) {
model.put("siteName", "CodeJava.net");
model.put("pageviews", 320000);
return"Stats";
// check login status....
if (!isLogin) {
returnnew ModelAndView("redirect:/login");
// return a list of Users
publicclassRegistrationController {
@RequestMapping(value = "/doRegister", method = RequestMethod.POST)
publicString doRegister(
@ModelAttribute("userForm") User user, BindingResult bindingResult) {
if (bindingResult.hasErrors()) {
// form validation error
} else {
// form input is OK
// process registration...
return"Success";
@RequestMapping(value = "/uploadFiles", method = RequestMethod.POST)
publicStringhandleFileUpload(
@RequestParam CommonsMultipartFile[] fileUpload) throws Exception {
for (CommonsMultipartFile aFile : fileUpload){
// stores the uploaded file
aFile.transferTo(new File(aFile.getOriginalFilename()));
@Autowired
private UserDAO userDAO;
publicString listUser() {
// handler method to list all users
userDAO.list();
publicString saveUser(User user) {
// handler method to save/update a user
userDAO.save(user);
publicString deleteUser(User user) {
// handler method to delete a user
userDAO.delete(user);
publicString getUser(int userId) {
// handler method to get a user
userDAO.get(userId);
interfaceUserDAO {
List<User> list();
void save(User user);
void checkLogin(User user);
List<User> listUser = userDAO.list();
@RequestMapping("/download")
publicStringdoDownloadFile(
HttpServletRequest request, HttpServletResponse response) {
// access the request
// access the response
return"DownloadPage";
publicclassProductController {
publicclassOrderController {
publicclassPaymentController {
欢迎在留言区留下你的观点,一起讨论提高。如果今天的文章让你有新的启发,学习能力的提升上有新的认识,欢迎转发分享给更多人。
欢迎各位读者加入程序员小乐技术群,在公众号后台回复“加群”或者“学习”即可。
猜你还想看
阿里、腾讯、百度、华为、京东最新面试题汇集
Java8 中用法优雅的 Stream,性能也"优雅"吗?
106页的《Python进阶》中文版(附下载)
红黑树的理解与Java实现
文章有问题?点此查看未经处理的缓存